package com.test.thread;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/8/23
 * Time: 16:57
 * To change this template use Editor | File and Code Templates.
 */
public class MainThread {

    public static void main(String[] args) throws InterruptedException {

//        int[] arrays = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//        T1 t1 = new T1(arrays);
//        Thread thread1 = new Thread(t1, "A");
//        Thread thread2 = new Thread(t1, "B");
//        Thread thread3 = new Thread(t1, "C");
//        thread1.start();
//        thread2.start();
//        thread3.start();
        Company c1 = new Company();
        Company c2 = new Company();
        c1.setName("c1");
        c2.setName("c2");
        c1.start();
        c2.start();

    }

    static class T1 extends Thread {

        int[] arrays;
        int index = 0;

        public T1(int[] a) {
            arrays = a;
        }

        @Override
        synchronized public void run() {
            while (index < arrays.length) {
                Test2 test2 = new Test2();
                test2.printString(arrays);
                index++;
            }
        }
    }

    static class Test2 {

        int index = 0;

        public void printString(int[] arrays) {
            synchronized (arrays) {
                if (index < arrays.length) {
                    System.out.println("线程" + Thread.currentThread().getName() + ": " + arrays[index]);
                    index++;
                }
            }
        }
    }


}
